package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.config.CacheService;
import com.heima.behavior.service.BehaviorService;
import com.heima.model.behavior.dtos.BehaviorDto;
import com.heima.model.behavior.dtos.BehaviorReadDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import thread.BhThreadLocalUtil;

@Service
public class BehaviorServiceImpl implements BehaviorService {


    private final CacheService cacheService;

    public BehaviorServiceImpl(CacheService cacheService) {
        this.cacheService = cacheService;

    }


    /**
     * 点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        //判断参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getOperation() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //token的id
        Integer userId = BhThreadLocalUtil.getUser().getId();

        if (userId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //redis的key
        String key = userId + "-" + dto.getArticleId();

        //redis是否存在对应的key
        Boolean exists = cacheService.exists(key);

        BehaviorDto behaviorDto;
        if (exists){
            String redisKey = cacheService.get(key);
            behaviorDto=JSON.parseObject(redisKey,BehaviorDto.class);
        }else {
            behaviorDto = new BehaviorDto();
        }

        //点赞参数判断
        if (dto.getOperation()==0){
            //点赞
            behaviorDto.setIslike(true);
        }else if (dto.getOperation()==1){
            //取消点赞
            behaviorDto.setIslike(false);
        }else {
            //参数错误
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //将值重新存入当前key
        cacheService.set(key,JSON.toJSONString(behaviorDto));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(BehaviorReadDto dto) {

        //判断参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getCount() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //token的id
        Integer userId = BhThreadLocalUtil.getUser().getId();

        if (userId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //redis的key
        String key = userId + "-" + dto.getArticleId();


        //redis是否存在对应的key
        Boolean exists = cacheService.exists(key);

        BehaviorDto behaviorDto;
        if (exists){
            String redisKey = cacheService.get(key);
            behaviorDto=JSON.parseObject(redisKey,BehaviorDto.class);
        }else {
            behaviorDto = new BehaviorDto();
        }

        Integer count=dto.getCount();
        ++count;
        behaviorDto.setCount(count);

        //将值重新存入当前key
        cacheService.set(key,JSON.toJSONString(behaviorDto));


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        //判断参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getType() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //token的id
        Integer userId = BhThreadLocalUtil.getUser().getId();

        if (userId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //redis的key
        String key = userId + "-" + dto.getArticleId();


        //redis是否存在对应的key
        Boolean exists = cacheService.exists(key);

        BehaviorDto behaviorDto;
        if (exists){
            String redisKey = cacheService.get(key);
            behaviorDto=JSON.parseObject(redisKey,BehaviorDto.class);
        }else {
            behaviorDto = new BehaviorDto();
        }

        //不喜欢参数判断
        if (dto.getType()==0){
            //不喜欢
            behaviorDto.setIsunlike(true);
        }else if (dto.getType()==1){
            //取消不喜欢
            behaviorDto.setIsunlike(false);
        }else {
            //参数错误
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //将值重新存入当前key
        cacheService.set(key,JSON.toJSONString(behaviorDto));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
